﻿

using System.Drawing;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using Styx;
    using Styx.CommonBot.Database;
    using Styx.Helpers;
    using Styx.Loaders;
    using Styx.CommonBot.AreaManagement;
    using Styx.CommonBot.AreaManagement.Triangulation;
    using Styx.TreeSharp;
    using Styx.CommonBot.Inventory;
    using Styx.CommonBot.Frames;
    using Styx.Pathing;
    using Styx.Pathing.OnDemandDownloading;
    using Styx.Patchables;
    using Styx.Plugins;
    using Styx.WoWInternals;
    using Styx.WoWInternals.Misc;
    using Styx.WoWInternals.Misc.DBC;
    using Styx.WoWInternals.World;
    using Styx.WoWInternals.WoWCache;
    using Styx.WoWInternals.WoWObjects;
    using System.Diagnostics;

namespace AzeniusHelper
{
    public class AzeniusHelper : HBPlugin
    {

        #region Boring Plugin Info

        public override string Name { get { return "Pandaria Dailies"; } }
        public override string Author { get { return "Megser"; } }
        public override Version Version { get { return new Version(1,0,1); } }
        public override bool WantButton { get { return true; } }
        public override string ButtonText { get { return "No Config"; } }

        #endregion
		
		private static readonly LocalPlayer Me = StyxWoW.Me;
		private static Stopwatch GlobalTimer = new Stopwatch();
		
        #region List of WoWUnits and WoWGameObjects needed
		
		public List<WoWUnit> PandarenSpirit
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>().Where(u => u.Entry == 59231 && u.Distance < 5).OrderBy(u => u.Distance).ToList();
            }
        }
		
		public List<WoWUnit> ShaoTienMindbinder
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>().Where(u => u.Entry == 63221 && u.Distance < 5).OrderBy(u => u.Distance).ToList();
            }
        }
		
		public List<WoWUnit> ShaoTienSubjugator
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>().Where(u => u.Entry == 64201 && u.Distance < 5).OrderBy(u => u.Distance).ToList();
            }
        }
		
		public List<WoWUnit> BrazierFire
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>().Where(u => u.Entry == 63787 && u.Distance < 5).OrderBy(u => u.Distance).ToList();
            }
        }
		public List<WoWGameObject> ShaoCage
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWGameObject>().Where(u => u.Entry == 214948 && u.Distance < 5).OrderBy(u => u.Distance).ToList();
            }
        }
		//public List<WoWUnit> ShaoTienSoulcaller
      //  {
         //   get
         //   {
         //       return ObjectManager.GetObjectsOfType<WoWUnit>().Where(u => u.Entry == 63610 && u.Distance < 5).OrderBy(u => u.Distance).ToList();
         //   }
        //}
	//	public List<WoWGameObject> PosionTrap
     //   {
        //    get
        //    {
       //         return ObjectManager.GetObjectsOfType<WoWGameObject>().Where(u => u.Entry == 213303).OrderBy(ret => ret.Distance).ToList();
         //   }
       // }
		
		//public List<WoWUnit> DreadKunchong
       // {
         //   get
         //   {
          //      return ObjectManager.GetObjectsOfType<WoWUnit>()
            //                        .Where(u => u.Entry == 64717 && !u.IsDead)
             //                       .OrderBy(u => u.Distance).ToList();
          //  }
      //  }
		
        #endregion
		
		#region Some Quest Helper Functions
        
		public bool QuestCompletedAndHas(uint questId)
        {
            return Me.QuestLog.GetQuestById(questId) != null && Me.QuestLog.GetQuestById(questId).IsCompleted;
        }
		
        public bool IsOnQuest(uint questId)
        {
            return Me.QuestLog.GetQuestById(questId) != null && !Me.QuestLog.GetQuestById(questId).IsCompleted;
        }

        public bool QuestComplete(uint questId)
        {
            return Me.QuestLog.GetQuestById(questId).IsCompleted;
        }
        

        public bool QuestFailed(uint questId)
        {
            return Me.QuestLog.GetQuestById(questId).IsFailed;
        }

        public bool ItemOnCooldown(uint ItemId)
        {
            return Lua.GetReturnVal<bool>("GetItemCooldown(" + ItemId + ")", 0);

        }

        public void UseQuestItem(uint ItemId)
        {
            Lua.DoString("UseItemByName(" + ItemId + ")");
        }

        public void UseIfNotOnCooldown(uint ItemId)
        {
            if (!ItemOnCooldown(ItemId))
            {
                UseQuestItem(ItemId);
            }
        }

        public bool QuestObjectiveComplete(uint questId, int objectiveNum) {
            return (Lua.GetReturnVal<int>("a,b,c=GetQuestLogLeaderBoard(" + objectiveNum + ",GetQuestLogIndexByID(" + questId + "));if c==1 then return 1 else return 0 end", 0) == 1);
        }

        public bool TargetingNpc(uint npcId) {
            return Me.CurrentTarget.Entry == npcId;
        }

        public bool HasQuest(uint questId) {
            return Me.QuestLog.GetQuestById(questId) != null;
        }
		
        #endregion

        public override void Pulse()
        {
			
			if (IsOnQuest(30231))
            {
                UseIfNotOnCooldown(79344); // Pomharvest Firework
            }
			
			if (IsOnQuest(30320) && (PandarenSpirit.Count >= 1))
            {
                   PandarenSpirit[0].Target();
                   PandarenSpirit[0].Interact();
            }
			
			if (IsOnQuest(30192) && (BrazierFire.Count >= 1))
            {
                   BrazierFire[0].Interact();
            }
			
			if (IsOnQuest(30310))
            {
               if (Me.HasAura("Lightning Pool"))
                {
                    Lua.DoString("StrafeLeftStart()"); // Strafe Left if we're in a pool
                }
            }
			
			if (IsOnQuest(31758) && (ShaoCage.Count >= 1) )
            {
                   ShaoCage[0].Interact();
            }
			
			#region Quest 30289 - In Enemy Hands

            if (IsOnQuest(30289) && (ShaoTienSubjugator.Count >= 1) && ShaoTienSubjugator[0].IsAlive)
            {
				ShaoTienSubjugator[0].Target();
            }
            #endregion
			
			while (QuestCompletedAndHas(30246) && Me.Combat)
            {
			
				   WoWPoint GetOutOfCombat = new WoWPoint(956.5208, 2156.625, 363.1769);
			
                   Navigator.MoveTo(GetOutOfCombat);
            }
			
			if (IsOnQuest(30310))
            {
               if (Me.HasAura("Lightning Pool"))
                {
                    Lua.DoString("StrafeLeftStart()"); // Strafe Left if we're in a pool
                }
            }
			if (IsOnQuest(30251))
            {
               if (Me.HasAura("Caustic Pitch"))
                {
                    Lua.DoString("MoveForwardStart()"); // Strafe Left if we're in a pool
					Thread.Sleep(5000);
					Lua.DoString("MoveForwardStop()");
                }
            }
			
			//#region Quest 30293 - In Enemy Hands

           // if (IsOnQuest(30293) && (ShaoTienMindbinder.Count >= 1))
           // {
			//	ShaoTienMindbinder[0].Target();
           // }
           // #endregion
			
			//#region Quest 30305 - He Know's What He's Doing

           // if (IsOnQuest(30305) && (ShaoTienSoulcaller.Count >= 1) && !Me.Combat)
           // {
			//	Navigator.MoveTo(PosionTrap[0].Location);
            //}
            //#endregion


        }
    }
}

